<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TrendsTopic extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'trends_topic';
    protected $primaryKey = 'topic_id';

    public function getList(Request $request) {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $topic_name = $request->input('topic_name', '');
        $sort_begin = (int) $request->input('sort_begin', 0);
        $sort_end = (int) $request->input('sort_end', 100);
        $begin_date = $request->input('begin_date', '');
        $end_date = $request->input('end_date', '');
        $begin_date = strtotime($begin_date);
        $end_date = strtotime($end_date);

        $query = new self();

        if($topic_name) $query = $query->where('topic_name','LIKE',"{$topic_name}%");
        if($sort_begin >= 0 && $sort_end) $query = $query->whereBetween('sort',[ $sort_begin, $sort_end ]);
        if($sort_begin >=0 && !$sort_end) $query = $query->where('sort','>=', $sort_begin);
        if($sort_end && !$sort_begin) $query = $query->where('sort','<=', $sort_end);
        if($begin_date && $end_date) $query = $query->whereBetween('created_at',[ $begin_date, $end_date ]);
        if($begin_date >=0 && !$end_date) $query = $query->where('created_at','>=', $begin_date);
        if($end_date && !$begin_date) $query = $query->where('created_at','<=', $end_date);

        $data = DB::connection('api')->table( DB::raw("({$query->toSql()}) as zb_a") )
            ->mergeBindings($query->getQuery())
            ->leftJoin('trends_topic_join as b','a.topic_id', 'b.topic_id')
            ->leftJoin('trends_content as c','b.content_id','c.content_id')
            ->groupBy('a.topic_id')
            ->orderBy('sort', 'DESC')
            ->orderBy('a.topic_id', 'asc')
            ->selectRaw('count(*) topic_count,count(distinct zb_c.user_id) user_count,zb_a.*')
            ->paginate($limit, ['*'], 'page', $page);
        return $data;

        return $query->with(['trendsTopicJoin.trendsContent'])->orderBy('sort', 'DESC')->paginate($limit, ['*'], 'page', $page);
    }
    public function createOrUpdateData($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/trends_topic/upsert', $data, 'json', 'POST');
    }

    public function deleteData($id)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/trends_topic/remove', [ 'topic_id' => $id ], 'urlencoded', 'GET');
    }

    public function trendsTopicJoin() {
        return $this->hasMany(TrendsTopicJoin::class, 'topic_id', 'topic_id');
        return $this->hasManyThrough(TrendsTopicJoin::class,TrendsContentLike::class,'content_id','content_id','topic_id','id');
    }

    public function trendsContent() {
        return $this->belongsToMany(TrendsContent::class,'trends_topic_join','topic_id','content_id');
    }

}
